<template>
  <div>
    <div class="pwd">
      <el-row>
        <el-col :span="2">&nbsp;</el-col>
        <el-col :span="18">
          <el-form label-width="80px" size="medium" :rules="rules" :model="password">
            <el-form-item label="旧密码" prop="oldPassword">
              <el-input type="password" v-model="password.oldPassword"> </el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
              <el-input type="password" v-model="password.newPassword"> </el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input type="password" v-model="password.confirmPassword"> </el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="4">&nbsp;</el-col>
      </el-row>
      <p class="tip">{{wrongMessage}}</p>
    </div>
  </div>
</template>

<script>
export default {
    name: 'password-change',
    props: ['passwordChangeVisible'],
    data(){
        return {
            password: {
                oldPassword: '',
                newPassword: '',
                confirmPassword: '',
            },
            //密码等级的正则表达式
            regExp: null,
            rules: {
                oldPassword: [
                    { required: true, message: '请输入旧密码', trigger: 'blur' }
                ],
                newPassword: [
                    { required: true, message: '请输入新密码', trigger: 'blur' }
                ],
                confirmPassword: [
                    { required: true, message: '请确认密码', trigger: 'blur' }
                ],
            },
            wrongMessage: '',
        };
    },
    methods:{
        //由header中的确定按钮来触发
        submit(){
            this.wrongMessage = '';
            let pwd = {
                oldPassword: this.password.oldPassword,
                newPassword: this.password.newPassword
            };
            if(this.password.newPassword !== this.password.confirmPassword){
                this.wrongMessage = '两次输入的密码不一致';
                return false;
            } 
            this.$axios.get('/crux-uc/api/user/' + pwd.newPassword + '/validate').then( res => {
                //res.data值为true/false
                if( !res.data ){
                    this.wrongMessage = '密码等级过低';
                    return;
                }
                this.$axios.post('/crux-uc/api/user/changepassword?newPassword=' + this.password.newPassword + '&oldPassword=' +
            this.password.oldPassword).then( res => {
                    //console.log(res.data);
                    if(res.data === ''){
                        this.$message({
                            message: '密码修改成功',
                            type: 'success',
                        });

                        //修改成功后关闭密码框
                        // bus.$emit('closePasswordDialog');

                    } else if (res.data === '原密码错误'){
                        this.wrongMessage = '原密码错误';
                    }
                }).catch( err => {
                    console.log(err);
                });
            });
        }
    },
    created(){
        
    },
    watch: {
        passwordChangeVisible(newVal){
            if( newVal === true ){
                //清空密码框数据
                this.password = {
                    oldPassword: '',
                    newPassword: '',
                    confirmPassword: '',
                };
            }
        }
    }
};
</script>

<style lang="scss">
  .pwd {
    .el-form-item {
      margin-bottom: 30px;
    }
    .el-item{
      margin-bottom: 0;
    }
    .el-row {
      margin-bottom: 10px;
    }
  }
</style>
<style scoped>
.tip {
  color: red;
  text-align: center;
  margin: 0 100px;
  letter-spacing: 1px;
}
</style>